package com.interview.function;

import javax.sound.midi.Soundbank;
import javax.sound.sampled.Mixer;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 常用的流
 */
public class TestCase {
    public static void main(String[] args) {
        List<Student> studentList = Stream.of(new Student("路飞", 22, 175),
                new Student("红发", 40, 180),
                new Student("白胡子", 50, 185)).collect(Collectors.toList());


        List<Student> list = studentList.stream().filter(stu -> stu.getHeight() < 180).collect(Collectors.toList());
        System.out.println(studentList.toString());

        List<String> names = studentList.stream().map(student -> student.getName()).collect(Collectors.toList());
        System.out.println(names);

        List<Student> students = Stream.of(studentList, Arrays.asList(new Student("艾斯",25,183),new Student("雷利",4,176)))
                .flatMap(students1 -> students1.stream()).collect(Collectors.toList());
        System.out.println("==================");
        for (int i = 0; i < students.size(); i++) {
            System.out.println(students.get(i).getName());
        }

        //我们经常会在集合中求最大或最小值，使用流就很方便。及早求值。
        Optional<Student> max = students.stream().max(Comparator.comparing(stu -> stu.getAge()));
        Optional<Student> min = students.stream().min(Comparator.comparing(stu -> stu.getAge()));
        if(max.isPresent()){
            System.out.println(max.get().getAge());
        }
        if(min.isPresent()){
            System.out.println(min.get().getAge());
        }

        //count
        long count = students.stream().filter(stu -> stu.getAge() < 30).count();
        System.out.println(count);

        Integer reduce = Stream.of(1,2,3,4).reduce(5,(acc, x)->acc + x);
        System.out.println(reduce);
    }
}
